using System.Collections;
using Game.Core;
using Game.Main.Controller;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Game.Main.View
{
	public class MainView : BaseView
	{
		private const float MIN_LOGO_X = -20f;

		private const float MAX_LOGO_X = 20f;

		private const float MIN_BG_ICON_X = -30f;

		private const float MAX_BG_ICON_X = 30f;

		private const float BTN_START_MIN_VAL = 0.9f;

		private const float BTN_START_MAX_VAL = 1.1f;

		private const float BTN_START_TARGET_VAL = 1.3f;

		private SceneController clr;

		private Transform mask;

		private Transform logo;

		private float logoAnimTimer = 0.5f;

		private bool isLogoMoveLeft;

		private Transform bgIcon;

		private GameObject btnStart;

		private float startAnimTimer = 0.5f;

		private bool isStartAnimZoomOut;

		private bool isStartAnimPlaying = true;

		private bool isBtnRecommendShow;

		private float btnRecomendAnimTimer;

		private bool isBtnRecommendAppear;

		private Transform btnRecommend;

		private Image recomendImage;

		private string recommendUrl;

		public MainView(SceneController clr)
			: base("prefabs/ui/scene/mainview", Singleton<UIManager>.Instance.uiLayer)
		{
			viewName = "MainView";
			this.clr = clr;
		}

		protected override void OnLoaded()
		{
			mask = GetChild("Mask");
			logo = GetChild("Logo");
			bgIcon = GetChild("BGIcon");
			btnStart = GetChild("BtnStart").gameObject;
			UIEventSystem.Get(btnStart).AddEventListener(EventTriggerType.PointerDown, OnBtnStartDown);
			btnRecommend = GetChild("BtnRecommend");
			UIEventSystem.Get(btnRecommend.gameObject).AddEventListener(EventTriggerType.PointerDown, OnBtnRecommendClick);
			Client.Instance.StartCoroutine(MainLoop());
			Client.Instance.StartCoroutine(DownLoadText());
			Singleton<AudioManager>.Instance.PlayMusic("music_main_view");
		}

		private IEnumerator DownLoadText()
		{
			WWW www = new WWW("https://www.xihegame.com/links/main_google_play.txt");
			yield return www;
			if (www.isDone && www.error == null && transform != null)
			{
				JSONArray jsonArr = JSONNode.Parse(www.text).AsArray;
				string imageUrl = string.Empty;
				recommendUrl = string.Empty;
				for (int i = 0; i < jsonArr.Count; i++)
				{
					string text = jsonArr[i]["name"];
					if (text.Equals("hospital"))
					{
						imageUrl = jsonArr[i]["image"];
						recommendUrl = jsonArr[i]["url"];
						break;
					}
				}
				if (!string.IsNullOrEmpty(imageUrl) && !string.IsNullOrEmpty(recommendUrl))
				{
					WWW imageLoader = new WWW(imageUrl);
					yield return imageLoader;
					if (imageLoader.isDone && imageLoader.error == null && transform != null)
					{
						Texture2D texture = imageLoader.texture;
						recomendImage = btnRecommend.GetComponent<Image>();
						recomendImage.sprite = Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), Vector2.zero);
						recomendImage.gameObject.SetActive(true);
						isBtnRecommendShow = true;
						btnRecomendAnimTimer = 0f;
						isBtnRecommendAppear = false;
					}
					imageLoader.Dispose();
				}
			}
			www.Dispose();
		}

		private void OnBtnRecommendClick(BaseEventData evt)
		{
			Application.OpenURL(recommendUrl);
		}

		private IEnumerator MainLoop()
		{
			while (transform != null)
			{
				logoAnimTimer += Time.deltaTime / 3f;
				if (isLogoMoveLeft)
				{
					logo.SetLocalX(Mathf.Lerp(-20f, 20f, logoAnimTimer));
					bgIcon.SetLocalX(Mathf.Lerp(30f, -30f, logoAnimTimer));
				}
				else
				{
					logo.SetLocalX(Mathf.Lerp(20f, -20f, logoAnimTimer));
					bgIcon.SetLocalX(Mathf.Lerp(-30f, 30f, logoAnimTimer));
				}
				if (logoAnimTimer >= 1f)
				{
					logoAnimTimer = 0f;
					isLogoMoveLeft = !isLogoMoveLeft;
				}
				if (isBtnRecommendShow)
				{
					btnRecomendAnimTimer += Time.deltaTime * 1.5f;
					if (isBtnRecommendAppear)
					{
						SetGameObjectScale(btnRecommend.gameObject, Mathf.Lerp(1f, 1.1f, btnRecomendAnimTimer));
					}
					else
					{
						SetGameObjectScale(btnRecommend.gameObject, Mathf.Lerp(1.1f, 1f, btnRecomendAnimTimer));
					}
					if (btnRecomendAnimTimer >= 1f)
					{
						btnRecomendAnimTimer = 0f;
						isBtnRecommendAppear = !isBtnRecommendAppear;
					}
				}
				if (isStartAnimPlaying)
				{
					startAnimTimer += Time.deltaTime * 1.5f;
					if (isStartAnimZoomOut)
					{
						SetGameObjectScale(btnStart, Mathf.Lerp(1.1f, 0.9f, startAnimTimer));
					}
					else
					{
						SetGameObjectScale(btnStart, Mathf.Lerp(0.9f, 1.1f, startAnimTimer));
					}
					if (startAnimTimer >= 1f)
					{
						startAnimTimer = 0f;
						isStartAnimZoomOut = !isStartAnimZoomOut;
					}
				}
				yield return new WaitForEndOfFrame();
			}
		}

		private void OnBtnStartDown(BaseEventData evt)
		{
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			isStartAnimPlaying = false;
			Client.Instance.StartCoroutine(BtnStartZoomIn());
			mask.gameObject.SetActive(true);
		}

		private IEnumerator BtnStartZoomIn()
		{
			float curScale = btnStart.transform.localScale.x;
			while (transform != null)
			{
				curScale += Time.deltaTime;
				if (curScale >= 1.3f)
				{
					SetGameObjectScale(btnStart, 1.3f);
					Client.Instance.StartCoroutine(BtnStartZoomOut());
					clr.ChangeScene(SceneType.MAP_VIEW);
					break;
				}
				SetGameObjectScale(btnStart, curScale);
				yield return new WaitForEndOfFrame();
			}
		}

		private IEnumerator BtnStartZoomOut()
		{
			float curScale = btnStart.transform.localScale.x;
			while (transform != null)
			{
				curScale -= Time.deltaTime;
				if (curScale <= 0f)
				{
					SetGameObjectScale(btnStart, 0f);
					break;
				}
				SetGameObjectScale(btnStart, curScale);
				yield return new WaitForEndOfFrame();
			}
		}

		private void SetGameObjectScale(GameObject go, float val)
		{
			go.transform.localScale = new Vector3(val, val, val);
		}
	}
}
